﻿// (c) Norbert Huffschmid
// This source is subject to the Microsoft Public License (Ms-PL).
// Please see http://go.microsoft.com/fwlink/?LinkID=131993 for details.

using System;
using System.ComponentModel;
using System.Windows;
using System.Windows.Input;
using log4net;
using NProtocol;
using ShoutBox.Core;
using ShoutBox.Core.Resources;

namespace ShoutBox
{
    public partial class MainWindow : Window
    {
        private static readonly ILog log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

        private SystemTrayIcon systemTrayIcon;
        private WpfViewModel viewModel;


        public MainWindow()
        {
            log.Info("Constructor");

            InitializeComponent();

            try
            {
                this.viewModel = new WpfViewModel();
                this.viewModel.PropertyChanged += new PropertyChangedEventHandler(viewModel_PropertyChanged);
                this.viewModel.ShutDownRequestReceived += new EventHandler(viewModel_ShutDownRequestReceived);

                this.systemTrayIcon = new SystemTrayIcon(this.viewModel);
                this.systemTrayIcon.ToggleVisibility += new EventHandler(systemTrayIcon_ToggleVisibility);
                this.systemTrayIcon.ExitClicked += new EventHandler(systemTrayIcon_ExitClicked);

                this.shoutBoxesComboBox.SelectedItemChanged += new EventHandler(shoutBoxesComboBox_SelectedItemChanged);

                this.DataContext = this.viewModel;
            }
            catch (Exception ex)
            {
                log.Error(ex.Message, ex);
#if DEBUG
                MessageBox.Show(ex.Message, "ShoutBox", MessageBoxButton.OK, MessageBoxImage.Error);
#endif
                this.Close();
            }
        }


        private void shoutBoxesComboBox_SelectedItemChanged(object sender, EventArgs e)
        {
            this.viewModel.ShoutTarget = (ISessionMember) this.shoutBoxesComboBox.SelectedItem;
        }

        private void MainWindow_Closing(object sender, CancelEventArgs e)
        {
            log.Debug("MainWindow_Closing");

            if (this.systemTrayIcon != null)
                this.systemTrayIcon.Dispose();

            if (this.viewModel != null)
                this.viewModel.Dispose();
        }

        private void MainWindow_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            // trick to allow moving a window without titlebar
            DragMove();
        }

        private void viewModel_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            log.Debug("viewModel_PropertyChanged - property: " + e.PropertyName);

            if (e.PropertyName.Equals("ShoutBoxListenerState"))
            {
                if (this.viewModel.ShoutBoxListenerState == ListenerState.Active)
                {
                    if (this.systemTrayIcon.PopupOnReceivedShout == true)
                    {
                        if (this.Visibility != Visibility.Visible)
                        {
                            if (this.viewModel.CurrentShouters.Count == 1)
                            {
                                foreach (ISessionMember sessionMember in this.viewModel.SessionMembers)
                                {
                                    if (sessionMember.Name.Equals(this.viewModel.CurrentShouters[0]))
                                    {
                                        // preselect current shouter as new shout target
                                        this.shoutBoxesComboBox.SelectedItem = sessionMember;
                                        break;
                                    }
                                }
                            }

                            // TODO: make some WPF animation here ...
                            this.Visibility = Visibility.Visible;
                        }
                    }
                }
            }
        }

        private void viewModel_ShutDownRequestReceived(object sender, EventArgs e)
        {
            log.Debug("viewModel_ShutDownRequestReceived");

            this.Close();
        }

        private void systemTrayIcon_ToggleVisibility(object sender, EventArgs e)
        {
            // toggle windows visibility
            if (this.Visibility == Visibility.Visible)
            {
                this.Visibility = Visibility.Collapsed;
            }
            else
            {
                this.Visibility = Visibility.Visible;
            }
        }

        private void systemTrayIcon_ExitClicked(object sender, EventArgs e)
        {
            this.Close();
        }

        private void MinimizeButton_Click(object sender, RoutedEventArgs e)
        {
            this.Visibility = Visibility.Collapsed;
        }

        private void Window_IsVisibleChanged(object sender, DependencyPropertyChangedEventArgs e)
        {
            log.Debug("Window_IsVisibleChanged: " + this.Visibility.ToString());

            if (this.Visibility == Visibility.Visible)
            {
                if (this.systemTrayIcon != null)
                {
                    // change context menu
                    this.systemTrayIcon.SetToggleInfo(Strings.Minimize);
                }
            }
            else
            {
                if (this.speakButton.IsChecked == true)
                {
                    // do not allow "unvisible" recording for privacy reasons
                    //this.Visibility = Visibility.Visible; // does not work!
                    this.speakButton.IsChecked = false;
                }

                if (this.systemTrayIcon != null)
                {
                    // program is now running in system tray
                    this.systemTrayIcon.ShowHideInformation();

                    // change context menu
                    this.systemTrayIcon.SetToggleInfo(Strings.Open);
                }
            }
            
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            if (Application.Current.Properties["StartCollapsed"] != null)
            {
                if ((bool)Application.Current.Properties["StartCollapsed"] == true)
                {
                    this.Visibility = Visibility.Collapsed;
                }
            }
        }
    }
}
